﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ncp.Business;
using ncp.Business.Common;

namespace ncp.Web.cms
{
    public partial class Login : System.Web.UI.Page
    {
        public UserInfo userInfo = new UserInfo();
        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(Login));
            if (!IsPostBack)
            {

            }
        }

        //验证验证码是否正确
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string VerificationCode(string code)
        {
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            string verCode = Session["VerificationCode"] == null ? "" : Session["VerificationCode"].ToString();
            if (verCode.ToLower() == code.ToLower())
            {
                jsonResult.Add("isreload", true);
            }
            else
            {
                jsonResult.Add("isreload", false);
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string UserLogin(string account, string pwd)
        {
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            try
            {
                DataTable table = UserInfoBll.UserLogin(CommonBll.InputText(account, 50), EncryptHelper.EncryptString(pwd), CommonConst.DB_Name);
                if (table.Rows.Count > 0)
                {
                    userInfo.Email = table.Rows[0]["User_Email"] == DBNull.Value ? "" : table.Rows[0]["User_Email"].ToString();
                    userInfo.Id = table.Rows[0]["User_id"].ToString();
                    userInfo.RelName = table.Rows[0]["User_name"] == DBNull.Value ? "" : table.Rows[0]["User_name"].ToString();
                    userInfo.Status = table.Rows[0]["User_Status"].ToString();
                    userInfo.UName = table.Rows[0]["User_Account"].ToString();
                    userInfo.Computername = HttpContext.Current.Application["computername"].ToString();
                    userInfo.Computerip = HttpContext.Current.Application["Computerip"].ToString();
                    WebMasterCookie.AddCookie(userInfo, CommonConst.adminCookieName);
                    Dictionary<string, object> dicLoginLog = new Dictionary<string, object>();
                    dicLoginLog.Add("user_id", userInfo.Id);
                    dicLoginLog.Add("computername", userInfo.Computername);
                    dicLoginLog.Add("computerip", userInfo.Computerip);
                    LogLoginBll.AddLoginLog(dicLoginLog,CommonConst.DB_Name);
                    jsonResult.Add("isreload",true);
                    jsonResult.Add("msg", "/cms/Default.aspx");
                }
                else
                {
                    jsonResult.Clear();
                    jsonResult.Add("isreload", false);
                    jsonResult.Add("msg", "用户名或密码错误！");
                }
            }
            catch (Exception ex)
            {
                jsonResult.Clear();
                jsonResult.Add("isreload", false);
                jsonResult.Add("msg", ex.Message);
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";

        }
    }
}
